package servlet.demo;

import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Create By IntelliJ IDEA
 *
 * @Author: jsonor
 * @Create-Time: 2017/9/26 8:17
 * @Description: demo2页面
 */
public class DemoServlet2 extends DemoServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    // 获取ServletContext对象
    ServletContext ctx = getServletContext();

    // 读取去ServletContext对象中的Number数据
    Object o = ctx.getAttribute("number");

    // 如果数据不存在，则设置为0
    int number = o == null ? 0 : (Integer) o;
    // number = ((Integer) o).intValue();


    // 增加1后放回去
    ctx.setAttribute("number", ++number);

    // 给用户响应
    resp.getWriter().write("you are the " + number + " visitor");
  }
}
